/*
 * BanBu_MagicKitViewController
 * @description 本文件提供对话中的魔法工具箱的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供对话中的魔法工具箱的ViewController
 */

#import "BanBu_MagicKitViewController.h"
#import "BanBu_ImageView.h"
#import "BanBu_NavButton.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"
#import "ToolsCell.h"
#import "CreatGifViewController.h"
#import "SparkTextViewController.h"
#import "BanBu_MagicFaceViewController.h"
#import "BanBu_TranslateViewController.h"

@interface BanBu_MagicKitViewController ()

@end

@implementation BanBu_MagicKitViewController
-(void)viewWillAppear:(BOOL)animated
{
    [UIView setAnimationsEnabled:YES];
    [super viewWillAppear:animated];
}
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
//file://localhost/Users/banbu3/Desktop/Xcode5/iOS7%E9%80%82%E9%85%8D%E7%89%88/BanBu/Chat/UI/translate.png
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"magicTool", nil);
    
    self.tableView.backgroundColor = [UIColor colorWithRed:255.0/255 green:250.0/255 blue:240.0/255 alpha:1.0];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.allowsSelectionDuringEditing = YES;
    
    BanBu_ImageView *img = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable] autorelease];
    self.tableView.backgroundView = img;
    
//    [self createBackButton];
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

    NSMutableArray *nameArr = [NSMutableArray arrayWithObjects:NSLocalizedString(@"magicGifTitle", nil),NSLocalizedString(@"sparkBtn",nil),NSLocalizedString(@"translateLabel", nil),@"火星文",nil];
    [array addObject:nameArr];
    NSMutableArray *imageName = [NSMutableArray arrayWithObjects:@"magicface.png",@"shan.png",@"translate.png",@"sparkcell.png", nil];
    
    [array addObject:imageName];
    self.dataArr =[NSArray arrayWithArray:array];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)createBackButton
//{
//    BanBu_NavButton *btn_return=[[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeBack];
//    btn_return.position = ButtonPositionLeft;
//    [btn_return addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
//    btn_return.titleLabel.font = [UIFont systemFontOfSize:14];
//    
//    UIBarButtonItem *bar_itemreturn = nil;
//    
//    CGFloat btnLen = [NSLocalizedString(@"returnButton", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 30)].width;
//    
//    btn_return.frame=CGRectMake(0, 0, btnLen+20, 30);
//    
//    [btn_return setTitle:NSLocalizedString(@"returnButton", nil) forState:UIControlStateNormal];
//    
//    bar_itemreturn=[[[UIBarButtonItem alloc] initWithCustomView:btn_return] autorelease];
//    
//    self.navigationItem.leftBarButtonItem = bar_itemreturn;
//}

-(void)popself
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSString * language = [MyAppDataManager getLanguageSubmitToServer];
    if ([language isEqualToString:@"cn"])
    {
        return 4;
    }

    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdetify = @"toolscell";
    ToolsCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];
    
    if(cell == nil)
    {
        cell = [[[ToolsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdetify] autorelease];
    }
    
    cell.iView.image = [UIImage imageNamed:[[_dataArr objectAtIndex:1] objectAtIndex:[indexPath row]]];
    
    cell.tLabel.text = [[_dataArr objectAtIndex:0] objectAtIndex:indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row)
    {
        case 0://魔法表情
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES];
            self.navigationController.view.userInteractionEnabled = NO;
            
            NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
            
            [parDic setValue:[MyAppDataManager getLanguageSubmitToServer] forKey:@"lang"];
            //下载魔法表情包信息
            [AppComManager getBanBuData:BanBu_GetMagicGif_list par:parDic delegate:self];
        }
            break;
        case 1://闪光字
        {
            CreatGifViewController *gifView = [[CreatGifViewController alloc] init];
            gifView.chat = self.chat;
            [self.navigationController pushViewController:gifView animated:YES];
            [gifView release];
        }
            break;
        case 2:
        {
            BanBu_TranslateViewController *translate = [[BanBu_TranslateViewController alloc] init];
            translate.title = NSLocalizedString(@"translateLabel", nil);
            translate.chat = self.chat;
            [self.navigationController pushViewController:translate animated:YES];
            [translate release];
        }
            break;
        case 3:
        {
            SparkTextViewController *spark = [[SparkTextViewController alloc] init];
            spark.title = @"火星文";
            spark.chat = self.chat;
            [self.navigationController pushViewController:spark animated:YES];
            [spark release];
        }
            break;
        default:
            break;
    }
}

-(void)dealloc
{
    [_dataArr release],_dataArr = nil;
    [super dealloc];
}


- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    self.navigationController.view.userInteractionEnabled = YES;
    [TKLoadingView dismissTkFromView:self.navigationController.view animated:YES afterShow:0.0];
    if(error)
    {
        if([error.domain isEqualToString:BanBuDataformatError])
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"network_error", nil) activityAnimated:NO duration:2.0];
        }
        else
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"network_error", nil) activityAnimated:NO duration:2.0];
        }
        return;
    }
    
    if ([AppComManager respondsDic:resDic isFunctionData:BanBu_GetMagicGif_list])
    {
        if([[resDic valueForKey:@"ok"] boolValue])
        {
            BanBu_MagicFaceViewController * magicFace = [[BanBu_MagicFaceViewController alloc]init];
            magicFace.chat = self.chat;
            magicFace.magicFaceKindArray = [resDic valueForKey:@"list"];
                        
            [self.navigationController pushViewController:magicFace animated:YES];
            [magicFace release];
        }
        else
        {
            [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:[MyAppDataManager IsInternationalLanguage:[resDic valueForKey:@"error"]] activityAnimated:NO duration:1.0];
        }
    }
}
@end
